
package session.stateless;

import entity.Employee;
import entity.TodoList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class TodoListBean implements TodoListBeanLocal {

    @PersistenceContext
    private EntityManager em;
    @EJB
    private EmployeeBeanLocal employeeBeanLocal;
    
    public List<TodoList> getTodoLists(Long id)
    {
        Employee employee = em.find(Employee.class, id);
        Query q = em.createQuery("SELECT t FROM TodoList t WHERE t.employee=:employee");
        q.setParameter("employee", employee);
        return (List<TodoList>) q.getResultList();
    }

    public void createToDoList(String task, Long id)
    {
        Employee employee = em.find(Employee.class, id);
        
        TodoList todoList = new TodoList();
        
        todoList.setEmployee(employee);
        todoList.setTask(task);
        
        em.persist(todoList);
        em.flush();
    }
    
    public void editToDoList(String task, Long id)
    {
        TodoList todoList = em.find(TodoList.class, id);
        todoList.setTask(task);
        em.flush();
    }
    
    public void deleteToDoList(Long id)
    {
        TodoList todoList = em.find(TodoList.class, id);
        em.remove(todoList);
        em.flush();
    }
}
